package com.zdx.yswl.ui.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdx.yswl.App;
import com.zdx.yswl.R;
import com.zdx.yswl.base.KActivity;
import com.zdx.yswl.tool.IHandlerLikeNotify;
import com.zdx.yswl.tool.IHandlerNotify;

/**
 * 
 * 定义标题栏 和 进度条
 * 
 * @author kangpeng
 */
public abstract class BaseActivity extends KActivity implements IHandlerLikeNotify, IHandlerNotify
{

	/** 标题bar **/
	public LinearLayout navigation;
	/** 回退按钮 **/
	public ImageView mBack;
	/** 中间标题 **/
	public TextView mTitle;
	/** 右边 **/
	public ImageView mRight;

	public ImageView progressBar;

	/** 设置标题栏（可定制标题栏事件处理类） */
	protected void setHeader(View.OnClickListener backEvt, String titleName) {
		mBack = (ImageView) findViewById(R.id.nav_left);
		mTitle = (TextView) findViewById(R.id.nav_title);
		mRight = (ImageView) findViewById(R.id.nav_right);
		navigation = (LinearLayout) findViewById(R.id.navigation);

		if (mBack != null) {
			mBack.setVisibility(View.VISIBLE);
		}
		if (mRight != null) {
			mRight.setVisibility(View.INVISIBLE);
		}
		// 如果不定制事件处理类，默认退回主页
		if (backEvt == null) {
			backEvt = new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// Intent intent = new Intent(BaseActivity.this,
					// MainActivity.class);
					// startActivity(intent);
					// overridePendingTransition(
					// R.anim.back_left_in,R.anim.back_right_out);
					finish();
				}
			};
		}
		mBack.setOnClickListener(backEvt);
		// 设置标题名称
		mTitle.setText(titleName);

	}

	/** 设置标题栏 */
	protected void setHeader(String titleName)
    {
		setHeader(null, titleName);
	}

	protected void startProgress() {
		progressBar = (ImageView) findViewById(R.id.head_progressBar);
		if (progressBar != null) {
			progressBar.setVisibility(View.VISIBLE);
			// progressBar.setBackgroundResource(R.anim.progress_img);
			AnimationDrawable anim = (AnimationDrawable) progressBar.getBackground();
			anim.start();
		}

	}

	protected void closeProgress() {
		if (progressBar != null) {
			progressBar.setVisibility(View.GONE);
		}
	}

    /** 初始化设置，不要在这里写费时的操作 */
    protected abstract void initSettings();

    /** 初始化界面，不要在这里写费时的操作 */
    protected abstract void initUi();

    /** 保存设置，不要在这里写费时的操作 */
    protected abstract void saveSettings();

    /** 释放资源、解锁、删除不用的对象，不要在这里写费时的操作 */
    protected abstract void freeMe();

    protected ProgressDialog proDialog;
    protected Toast toast;

    private IHandlerNotify notify = this;
    protected MyHandler handler = new MyHandler(this);

    protected static class MyHandler extends Handler
    {

        private BaseActivity activity;

        public MyHandler(BaseActivity activity)
        {
            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg)
        {
            activity.notify.onHandler(msg.what, msg.arg1, msg.arg2, msg.obj);
            super.handleMessage(msg);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // ((App) getApplication()).push(this);
        ((App) getApplication()).setCurrentNotifyer(this);
    }

    @Override
    protected void onResume()
    {
    	super.onResume();
    	initSettings();
        initUi();
    }
    
    @Override
    protected void onDestroy()
    {
        // ((App) getApplication()).pop();
        freeMe();
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        saveSettings();
        super.onPause();
    }

    /**
     * 弹出progressDialog
     * @param msg
     * @param cancel  点击dialog区域外是否可以取消dialog显示
     * @param backcancel  点击back是否可以取消dialog显示
     */
    public void createDialog(String msg, boolean cancel, boolean backcancel)
    {

        if (null == msg || "".equalsIgnoreCase(msg))
        {
            msg = getResources().getString(R.string.waiting);
        }
        try
        {
            if (null != BaseActivity.this && !BaseActivity.this.isFinishing())
            {
                if (null == proDialog)
                {
                    proDialog = new ProgressDialog(BaseActivity.this);
                }
                proDialog.setMessage(msg);
                if (null != proDialog)
                {
                    if (null != BaseActivity.this && !BaseActivity.this.isFinishing())
                    {
                        proDialog.show();
                        proDialog.setCancelable(cancel);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        if(backcancel)
        {
            proDialog.setOnKeyListener(onKeyListener);
        }
    }

    /**
     * progressDialog返回键监听
     */
    private DialogInterface.OnKeyListener onKeyListener = new DialogInterface.OnKeyListener()
    {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent)
        {
            if(i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_DOWN)
            {
                dismissDialog();
            }
            return false;
        }
    };

    /**
     * 关闭dialog
     */
    public void dismissDialog()
    {
        if (null != proDialog)
        {
            proDialog.dismiss();
            proDialog = null;
        }
    }

    /**
     * 弹系统消息
     */
    public void showTextToast(int id)
    {
        String msg = getApplication().getResources().getString(id);
        if (toast == null) {
            toast = Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    /**
     * 弹系统消息
     * @param msg
     */
    public void showTextToast(String msg)
    {
        if (toast == null)
        {
            toast = Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT);
        }
        else
        {
            toast.setText(msg);
        }
        toast.show();
    }
}
